pub mod console_appender;
pub mod file_appender;
pub mod file_mmap_appender;

pub use self::console_appender::ConsoleAppender;
pub use self::file_appender::FileAppender;
pub use self::file_mmap_appender::FileMmapAppender;
use std::path::Path;

use crate::record;

#[derive(PartialEq)]
pub enum Appender {
    ConsoleAppender(ConsoleAppender),
    FileAppender(FileAppender),
    FileMmapAppender(FileMmapAppender),
}

impl Appender {
    pub fn new_console_appender() -> ConsoleAppender {
        ConsoleAppender::new()
    }

    pub fn new_file_appender<P: AsRef<Path>>(path: P) -> crate::Result<FileAppender> {
        FileAppender::new(path)
    }

    pub fn new_file_mmap_appender<P: AsRef<Path>>(
        path: P,
        mmap_size: u64,
    ) -> crate::Result<FileMmapAppender> {
        FileMmapAppender::new(path, mmap_size)
    }

    pub fn name(&self) -> &str {
        match self {
            Self::ConsoleAppender(ref appender) => appender.name(),
            Self::FileAppender(ref appender) => appender.name(),
            Self::FileMmapAppender(ref appender) => appender.name(),
        }
    }

    pub fn process(&mut self, record: &record::Record) -> crate::Result<()> {
        match self {
            Self::ConsoleAppender(ref mut appender) => appender.process(record),
            Self::FileAppender(ref mut appender) => appender.process(record),
            Self::FileMmapAppender(ref mut appender) => appender.process(record),
        }
    }
}
